﻿using NLog;
using System.Threading.Tasks;
using XCI.Core;

namespace XCI.Service
{
    /// <summary>
    /// 服务端系统操作接口
    /// </summary>
    public class CoreService
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
        private const string RootUrl = "/core";
        public static CoreService Instance { get; set; } = new CoreService();

        /// <summary>
        /// 客户端与服务端网络测试
        /// </summary>
        public async Task<BoolResult> PingAsync()
        {
            return await App.Client.PostAsBoolResultAsync($"{RootUrl}/ping");
        }

        /// <summary>
        /// 获取客户端IP地址
        /// </summary>
        public async Task<string> ClientIpAsync()
        {
            var result = await App.Client.PostAsBoolResultAsync<string>($"{RootUrl}/clientIP");
            if (result.Success)
            {
                return result.Data;
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取服务器日期和时间
        /// </summary>
        public async Task<string> DatetimeAsync()
        {
            var result = await App.Client.PostAsBoolResultAsync<string>($"{RootUrl}/datetime");
            if (result.Success)
            {
                return result.Data;
            }

            WriteLog(result);
            return string.Empty;
        }

        /// <summary>
        /// 创建新Id
        /// </summary>
        public async Task<string> NewIdAsync()
        {
            return await App.Client.PostAsync<string>($"{RootUrl}/newId");
        }

        private void WriteLog(BoolResult result)
        {
            if (result.Fail)
            {
                Logger.Error(result.Msg);
            }
        }
    }
}